public class Test {
    public int sqrt (int x) {
        // write code here
        if(x <= 0){
            return x;
        }
        if(x == 1 || x == 2){
            return 1;
        }
        int left = 1, right = x;
        int mid;
        while(left < right){
            mid = (left + right) / 2;
            if(mid * mid == x ||((mid*mid < x)&&((mid+1)*(mid+1)>x))){
                return mid;
            }else if(mid * mid > x){
                right = mid;
            }else{
                left = mid;
            }
        }
        return left;
    }
}
